import Phaser from "phaser";
import { MainScene } from "../scenes";

export class Polygon extends Phaser.Physics.Arcade.Sprite {

    health=100
    text:Phaser.GameObjects.Text
    row:number
    col:number
    declare scene: MainScene

    constructor(scene: MainScene, x: number, y: number, texture: string | Phaser.Textures.Texture,row:number,col:number) {
        super(scene, x, y, texture)
        scene.sys.displayList.add(this)
        scene.sys.updateList.add(this)
        scene.physics.world.enableBody(this,1)
        
        this.row=row
        this.col=col

        this.text=scene.add.text(this.x,this.y,this.health.toString())
        this.text.setOrigin(0.5,0.5)
        this.text.setColor('black')
    }

    protected preUpdate(time: number, delta: number): void {
        // console.log('Defender',this)
        super.preUpdate(time, delta)
        this.text.setText(this.health.toString())
        if(this.health<=0){
            this.scene.areas[this.row][this.col]=0
            this.destroy()
        }
    }

    destroy(fromScene?: boolean): void {
        this.text.destroy()
        super.destroy()
    }

}